package com.winderinfo.yikaotianxia.core.mvp;

import java.util.Map;

public class Presenter implements Contract.Presenter {
    //实例m
    public Model model;
    //实例v
    public Contract.View view;

    public Presenter(Contract.View view) {
        this.model = new Model();
        this.view = view;
    }

    @Override
    public void onGetRequest(String url, Map<String, Object> map, Class kind) {
        model.get(url, map, kind, new Contract.CallBack() {
            @Override
            public void onSuccess(Object data) {
                view.onSuccess(data);
            }

            @Override
            public void onFailure(Object error) {
                view.onFailure(error);
            }
        });
    }

    @Override
    public void onPostRequest(String url, Map<String, Object> map, Class kind) {
        model.post(url, map, kind, new Contract.CallBack() {
            @Override
            public void onSuccess(Object data) {
                view.onSuccess(data);
            }

            @Override
            public void onFailure(Object error) {
                view.onFailure(error);
            }
        });
    }

    @Override
    public void onPutRequest(String url, Map<String, Object> map, Class kind) {
        model.put(url, map, kind, new Contract.CallBack() {
            @Override
            public void onSuccess(Object data) {
                view.onSuccess(data);
            }

            @Override
            public void onFailure(Object error) {
                view.onFailure(error);
            }
        });
    }

    @Override
    public void onDeleteRequest(String url, Map<String, Object> map, Class kind) {
        model.delete(url, map, kind, new Contract.CallBack() {
            @Override
            public void onSuccess(Object data) {
                view.onSuccess(data);
            }

            @Override
            public void onFailure(Object error) {
                view.onFailure(error);
            }
        });
    }

    @Override
    public void imagePost(String url, Map<String, String> map, Class kind) {
        model.imagePast(url, map, kind, new Contract.CallBack() {
            @Override
            public void onSuccess(Object data) {
                view.onSuccess(data);
            }

            @Override
            public void onFailure(Object error) {
                view.onFailure(error);
            }
        });
    }

    //解决内存泄露
    public void onUntying() {
        if (model != null) {
            model = null;
        }
        if (view != null) {
            view = null;
        }
    }

}